﻿using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EPiServer;
using TemplateFoundation.Exceptions;
using TemplateFoundation.PageTypes;

namespace TemplateFoundation.WebControls
{
    /// <summary>
    /// Base class for creating new web controls
    /// </summary>
    public abstract class WebControlBase : WebControl
    {
        // Private, we should always specify a tag
        private WebControlBase()
        {
            
        }

        /// <summary>
        /// Creates a new control using the specified HTML tag
        /// </summary>
        /// <param name="tag"></param>
        public WebControlBase(HtmlTextWriterTag tag) : base(tag)
        {
            
        }

        /// <summary>
        /// Gets the current page.
        /// </summary>
        /// <value>The current page.</value>
        protected PageTypeBase CurrentPage
        {
            get
            {
                var pb = HttpContext.Current.Handler as PageBase;

                if(pb==null)
                {
                    throw new TemplateFoundationException("Unable to identify current page");
                }

                return (PageTypeBase)pb.CurrentPage;
            }
        }
    }
}
